package controllers.cart;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import lib_model.Cart.Cart;
import lib_model.Cart.ItemCart;
import lib_model.Product.Products;

/**
 * Servlet implementation class Add
 */
@WebServlet("/Cart/AddItemCart")
public class AddItemCart extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public AddItemCart() {
		super();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String IDProduct = request.getParameter("id");
		System.out.println("ID SAN PHAM : " +IDProduct);
		HttpSession hs = request.getSession();
		if (IDProduct == "" || IDProduct == null) {

		} else {
			Products p = new Products();
			p.id = Integer.parseInt(IDProduct);
			p = (Products)(p.GetInformation().ObjResult);
			Cart ct = new Cart();
			ItemCart item = new ItemCart();
			item.id = Integer.parseInt(IDProduct);
			item.product = p;
			item.quantity = 1;
			ct.AddItemCart(item);
			if (hs.getAttribute("Cart") == null) {
				hs.setAttribute("Cart", ct);
			} else {
				((Cart) hs.getAttribute("Cart")).AddItemCart(item);
			}
		}
		response.sendRedirect("../pages/cart.jsp");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
	}

}
